//
//  ViewController.m
//  TestTableHeaderFooterView
//
//  Created by 花强 on 2017/11/30.
//  Copyright © 2017年 花强. All rights reserved.
//

#import "ViewController.h"

#import "HQPulldownTableViewCell.h"

#define COLOR_RGBA(r, g, b, a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
#define COLOR_RGB(r, g, b) COLOR_RGBA((r), (g), (b), 1)

#define COLOR_RGB_HEX(rgbValue) COLOR_RGB((((rgbValue) & 0xFF0000) >> 16), (((rgbValue) & 0xFF00) >> 8), (((rgbValue) & 0xFF)))

#define kWidth            [UIScreen mainScreen].bounds.size.width
#define kHeight           [UIScreen mainScreen].bounds.size.height

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic , strong) NSMutableArray * tableArrayForAll;
@property (nonatomic , strong) NSMutableArray * tableArrayForSections;

@property (nonatomic , strong) UITableView * tableView;

@end

@implementation ViewController
-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        [_tableView setBackgroundColor:[UIColor clearColor]];
        _tableView.showsVerticalScrollIndicator = NO;
        
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
-(NSMutableArray *)tableArrayForSections{
    if (_tableArrayForSections == nil) {
        _tableArrayForSections = [NSMutableArray array];
    }
    return _tableArrayForSections;
}
-(NSMutableArray *)tableArrayForAll{
    if (_tableArrayForAll == nil) {
        _tableArrayForAll = [NSMutableArray array];
    }
    return _tableArrayForAll;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self.tableView setFrame:CGRectMake(0, 0, kWidth, kHeight)];
    [self.view addSubview:self.tableView];
    self.tableView.tableFooterView = nil;
    
    NSArray * array0 = @[@{@"title":@"zero",@"height":@"60"}];
    NSArray * array1 = @[@{@"title":@"a",@"height":@"200"},@{@"title":@"a1",@"height":@"120"}];
    NSArray * array2 = @[@{@"title":@"b",@"height":@"200"},@{@"title":@"b1",@"height":@"120"}];
    NSArray * array3 = @[@{@"title":@"c",@"height":@"180"},@{@"title":@"c1",@"height":@"120"}];

    [self.tableArrayForAll addObject:array0];
    [self.tableArrayForAll addObject:array1];
    [self.tableArrayForAll addObject:array2];
    [self.tableArrayForAll addObject:array3];

    NSMutableDictionary * dic1 = [NSMutableDictionary dictionaryWithDictionary:@{@"title":@"a",@"flag":@"0"}];
    NSMutableDictionary * dic2 = [NSMutableDictionary dictionaryWithDictionary:@{@"title":@"b",@"flag":@"0"}];

    [self.tableArrayForSections addObject:dic1];
    [self.tableArrayForSections addObject:dic2];
    
    [self.tableView reloadData];

}
-(void)openBtnClicked:(UIButton *)btn{
    NSMutableDictionary * dic = self.tableArrayForSections[btn.tag - 1 - 1];
    NSString * flag = dic[@"flag"];
    if ([flag isEqualToString:@"0"]) {
        [dic setValue:@"1" forKey:@"flag"];
    }else{
        [dic setValue:@"0" forKey:@"flag"];
    }
    //一个section刷新
    NSIndexSet *indexSet=[[NSIndexSet alloc] initWithIndex:(btn.tag-1)];
    [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
}
#pragma mark - ViewDelegate(例UITableView)
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    //* 设置系统默认分割线从边线开始(2)
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {//ios7
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 45, 0, 0)];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {//ios8 or later
        [cell setLayoutMargins:UIEdgeInsetsMake(0, 45, 0, 0)];
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary * dic = self.tableArrayForAll[indexPath.section][indexPath.row];
    NSString * height = dic[@"height"];
    
    return  [height floatValue];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }else if (section == self.tableArrayForAll.count - 1){
        return 20;
    }
    return 45;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return nil;
    }else if (section == self.tableArrayForAll.count - 1){
        UIView * view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 20)];
        [view1 setBackgroundColor:[UIColor yellowColor]];
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kWidth, 20)];
        [label setText:@"c"];
        [view1 addSubview:label];
        return view1;
    }
    NSDictionary * dic = self.tableArrayForSections[section - 1];

    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 45)];
    [view setBackgroundColor:[UIColor redColor]];
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kWidth, 45)];
    [label setText:dic[@"title"]];
    
    [view addSubview:label];
    
    UIButton *openBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kWidth, 45)];
    openBtn.tag = section + 1;
    [openBtn addTarget:self action:@selector(openBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:openBtn];
    
    return view;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
#pragma mark - DataSourceDelegate(例UITableViewDataSource)
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    
    
    HQPulldownTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HQPulldownTableViewCell"];
    if (cell==NULL) {
        cell = [HQPulldownTableViewCell newCell];
        
    }
    
    cell.accessoryType = UITableViewCellAccessoryNone;//next
    
    NSDictionary * dic = self.tableArrayForAll[indexPath.section][indexPath.row];
    cell.hqtitle.text = dic[@"title"];
    
    return cell;
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.tableArrayForAll.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return [self.tableArrayForAll[section] count];
    }else if (section == self.tableArrayForAll.count - 1){
        return [self.tableArrayForAll[section] count];
    }
    NSDictionary * dic = self.tableArrayForSections[section - 1];
    if ([dic[@"flag"] isEqualToString:@"0"]) {
        return 0;
    }
    return [self.tableArrayForAll[section] count];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
